package com.module.check.manager;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.check.dao.CheckTaskDao;
import com.module.check.util.ReturnUtil;
import com.module.check.vo.CheckTask;

public class CheckTaskManager extends GenericHibernateManager<CheckTask, Long> {
	
	private CheckTaskManager(){}
	
	private static CheckTaskManager manager;
	
	public synchronized static CheckTaskManager getInstance(){
		if(manager == null)
			manager = new CheckTaskManager();
		return manager;
	}
	
	private CheckTaskDao dao = CheckTaskDao.getInstance();
	
	/**
	 * 查询稽查任务列表
	 * @param pageObj
	 * @param pu
	 * @return
	 * @throws Exception
	 */
	public Page list(Page pageObj,CheckTask pu) throws Exception{
		Session session = this.getSession();
		Page page = new Page();
		try {
			page = dao.getList(session,pu, pageObj.getStart(),pageObj.getLimit());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}
	/**
	 * 新增/修改
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	 public Map<String,Object> add(CheckTask obj) throws Exception
	    {
	    	Session session = this.getSession();
	    	Transaction tx =session.beginTransaction();
	    	boolean flag = false;
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	try{
	    		 if(null!=obj.getId()&&0!=obj.getId()){//修改
//	                 obj.setStatus(CUDEnum.U.toString());
	                 obj.setCreateTime(new Date());
	                 flag = dao.update(session, obj);

	             }else{
	                 obj.setStatus(CUDEnum.C.toString());
	                 obj.setCreateTime(new Date());
	                 flag = dao.save(session, obj);
	             }
	    		tx.commit();
	    	}catch(Exception e){
	    		tx.rollback();
	    		throw e;	    		
	    	}finally{
	    		this.closeSession();
	    	}
	    	map = ReturnUtil.optHandle(flag);
	    	return map;
	    }
	/**
	 * 删除任务
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	  public Map<String,Object> delete(Long id) throws Exception
	    {
	    	Session session = this.getSession();
	    	Transaction tx =session.beginTransaction();
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	boolean flag = false;
	    	try{
	    		CheckTask obj = dao.get(session, id);
	    		obj.setStatus(CUDEnum.D.toString());
	    		flag = dao.update(session, obj);
	            tx.commit();
	    	}catch(Exception e){
	            tx.rollback();
	            throw e;
	            
	    	}finally{
	    		this.closeSession();
	    	}
	    	map = ReturnUtil.optHandle(flag);
	    	return map;

	    }
	  /**
	   * 任务分配
	   * @param id
	   * @return
	   * @throws Exception
	   */
	  public Map<String,Object> taskAllot(Long id) throws Exception
	  {
		  Session session = this.getSession();
		  Transaction tx =session.beginTransaction();
		  Map<String,Object> map = new HashMap<String,Object>();
		  boolean flag = false;
		  try{
			  CheckTask obj = dao.get(session, id);
			  obj.setStatus(CUDEnum.U.toString());
			  flag = dao.update(session, obj);
			  tx.commit();
		  }catch(Exception e){
			  tx.rollback();
			  throw e;
			  
		  }finally{
			  this.closeSession();
		  }
		  map = ReturnUtil.optHandle(flag);
		  return map;
		  
	  }
	  /**
	   * 根据id查找
	   * @param id
	   * @return
	   * @throws Exception
	   */
	   public CheckTask findById(Long id) throws Exception{
		   Session session = this.getSession();
		   CheckTask vo = new CheckTask();
		   try{
			   vo = dao.get(session, id);
		   }catch (Exception e) {
				throw e;
		   }finally{
			   this.closeSession();
		   }
		   return vo;
	   }
	   
	@Override
	public GenericHibernateDao<CheckTask, Long> getDao() {
		// TODO Auto-generated method stub
		return null;
	}
}
